package databasetest;

import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;

import java.awt.Color;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.event.InternalFrameAdapter;
import javax.swing.event.InternalFrameEvent;

public class Insert extends JInternalFrame {
	
	Connection conn;
	DatabaseTest dt;
	
	private JLabel label;
	private JTextField code;
	private JLabel lblNewLabel;
	private JTextField ea;
	private JButton btnNewButton;
	private JLabel result;

	
	public Insert(Connection c, DatabaseTest dt) {
		super("Insert", true, true, true, false);
		addInternalFrameListener(new InternalFrameAdapter() {
			@Override
			public void internalFrameActivated(InternalFrameEvent e) {
				code.setText("");
				ea.setText("");
				result.setText("코드를 입력해 주세요.");
				code.requestFocus();
			}
		});
		
		this.conn = c;
		this.dt = dt;
		setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		setBounds(0, 0, 450, 300);
		getContentPane().setLayout(null);
		getContentPane().add(getLabel());
		getContentPane().add(getCode());
		getContentPane().add(getLblNewLabel());
		getContentPane().add(getEa());
		getContentPane().add(getBtnNewButton());
		getContentPane().add(getResult());
		
		
		

	}
	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("코드");
			label.setBounds(117, 34, 30, 20);
		}
		return label;
	}
	private JTextField getCode() {
		if (code == null) {
			code = new JTextField();
			code.setBounds(159, 34, 116, 21);
			code.setColumns(10);
		}
		return code;
	}
	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("수량");
			lblNewLabel.setBounds(117, 86, 30, 20);
		}
		return lblNewLabel;
	}
	private JTextField getEa() {
		if (ea == null) {
			ea = new JTextField();
			ea.setBounds(159, 86, 116, 21);
			ea.setColumns(10);
		}
		return ea;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("저장");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					try{
							String c = code.getText();
							int a = Integer.parseInt(ea.getText());
							
							String sql = "insert into ptbl(serial, code, ea) values(ptbl_serial.nextval, ?, ?)";
							// Connection객체로부터 연결된 오라클서버에 sql문장을 실행
							PreparedStatement pstmt = conn.prepareStatement(sql);
							pstmt.setString(1, c);
							pstmt.setInt(2, a);
							pstmt.executeUpdate();
							
							result.setText("저장 되었습니다.");
							code.setText("");
							ea.setText("");
							code.requestFocus();
							
							dt.select.setList();
							
					}catch(Exception ex){
						
					}
				}
			});
			btnNewButton.setBounds(168, 140, 97, 23);
		}
		return btnNewButton;
	}
	private JLabel getResult() {
		if (result == null) {
			result = new JLabel("코드를 입력해 주세요.");
			result.setBackground(Color.WHITE);
			result.setOpaque(true);
			result.setBounds(10, 238, 412, 25);
		}
		return result;
	}
}
